home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / update / strtoul.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-02-19  |  4.3 KB  |  193 lines

  1. /* 
  2.  * strtoul.c --
  3.  *
  4.  *    Source code for the "strtoul" library procedure.
  5.  *
  6.  * Copyright 1988 Regents of the University of California
  7.  * Permission to use, copy, modify, and distribute this
  8.  * software and its documentation for any purpose and without
  9.  * fee is hereby granted, provided that the above copyright
  10.  * notice appear in all copies.  The University of California
  11.  * makes no representations about the suitability of this
  12.  * software for any purpose.  It is provided "as is" without
  13.  * express or implied warranty.
  14.  */
  15.  
  16. #ifndef lint
  17. static char rcsid[] = "$Header: /sprite/src/lib/c/stdlib/RCS/strtoul.c,v 1.2 89/03/22 00:47:33 rab Exp $ SPRITE (Berkeley)";
  18. #endif /* not lint */
  19.  
  20. #include <stdlib.h>
  21. #include <ctype.h>
  22.  
  23. #define NULL 0
  24. #define FALSE 0
  25. #define TRUE 1
  26. /*
  27.  * The table below is used to convert from ASCII digits to a
  28.  * numerical equivalent.  It maps from '0' through 'z' to integers
  29.  * (100 for non-digit characters).
  30.  */
  31.  
  32. static char cvtIn[] = {
  33.     0, 1, 2, 3, 4, 5, 6, 7, 8, 9,        /* '0' - '9' */
  34.     100, 100, 100, 100, 100, 100, 100,        /* punctuation */
  35.     10, 11, 12, 13, 14, 15, 16, 17, 18, 19,    /* 'A' - 'Z' */
  36.     20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
  37.     30, 31, 32, 33, 34, 35,
  38.     100, 100, 100, 100, 100, 100,        /* punctuation */
  39.     10, 11, 12, 13, 14, 15, 16, 17, 18, 19,    /* 'a' - 'z' */
  40.     20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
  41.     30, 31, 32, 33, 34, 35};
  42.  
  43. /*
  44.  *----------------------------------------------------------------------
  45.  *
  46.  * strtoul --
  47.  *
  48.  *    Convert an ASCII string into an integer.
  49.  *
  50.  * Results:
  51.  *    The return value is the integer equivalent of string.  If endPtr
  52.  *    is non-NULL, then *endPtr is filled in with the character
  53.  *    after the last one that was part of the integer.  If string
  54.  *    doesn't contain a valid integer value, then zero is returned
  55.  *    and *endPtr is set to string.
  56.  *
  57.  * Side effects:
  58.  *    None.
  59.  *
  60.  *----------------------------------------------------------------------
  61.  */
  62.  
  63. unsigned long int
  64. strtoul(string, endPtr, base)
  65.     char *string;        /* String of ASCII digits, possibly
  66.                  * preceded by white space.  For bases
  67.                  * greater than 10, either lower- or
  68.                  * upper-case digits may be used.
  69.                  */
  70.     char **endPtr;        /* Where to store address of terminating
  71.                  * character, or NULL. */
  72.     int base;            /* Base for conversion.  Must be less
  73.                  * than 37.  If 0, then the base is chosen
  74.                  * from the leading characters of string:
  75.                  * "0x" means hex, "0" means octal, anything
  76.                  * else means decimal.
  77.                  */
  78. {
  79.     register char *p;
  80.     register unsigned long int result = 0;
  81.     register unsigned digit;
  82.     int anyDigits = FALSE;
  83.  
  84.     /*
  85.      * Skip any leading blanks.
  86.      */
  87.  
  88.     p = string;
  89.     while (isspace(*p)) {
  90.     p += 1;
  91.     }
  92.  
  93.     /*
  94.      * If no base was provided, pick one from the leading characters
  95.      * of the string.
  96.      */
  97.     
  98.     if (base == 0)
  99.     {
  100.     if (*p == '0') {
  101.         p += 1;
  102.         if (*p == 'x') {
  103.         p += 1;
  104.         base = 16;
  105.         } else {
  106.  
  107.         /*
  108.          * Must set anyDigits here, otherwise "0" produces a
  109.          * "no digits" error.
  110.          */
  111.  
  112.         anyDigits = TRUE;
  113.         base = 8;
  114.         }
  115.     }
  116.     else base = 10;
  117.     } else if (base == 16) {
  118.  
  119.     /*
  120.      * Skip a leading "0x" from hex numbers.
  121.      */
  122.  
  123.     if ((p[0] == '0') && (p[1] == 'x')) {
  124.         p += 2;
  125.     }
  126.     }
  127.  
  128.     /*
  129.      * Sorry this code is so messy, but speed seems important.  Do
  130.      * different things for base 8, 10, 16, and other.
  131.      */
  132.  
  133.     if (base == 8) {
  134.     for ( ; ; p += 1) {
  135.         digit = *p - '0';
  136.         if (digit > 7) {
  137.         break;
  138.         }
  139.         result = (result << 3) + digit;
  140.         anyDigits = TRUE;
  141.     }
  142.     } else if (base == 10) {
  143.     for ( ; ; p += 1) {
  144.         digit = *p - '0';
  145.         if (digit > 9) {
  146.         break;
  147.         }
  148.         result = (10*result) + digit;
  149.         anyDigits = TRUE;
  150.     }
  151.     } else if (base == 16) {
  152.     for ( ; ; p += 1) {
  153.         digit = *p - '0';
  154.         if (digit > ('z' - '0')) {
  155.         break;
  156.         }
  157.         digit = cvtIn[digit];
  158.         if (digit > 15) {
  159.         break;
  160.         }
  161.         result = (result << 4) + digit;
  162.         anyDigits = TRUE;
  163.     }
  164.     } else {
  165.     for ( ; ; p += 1) {
  166.         digit = *p - '0';
  167.         if (digit > ('z' - '0')) {
  168.         break;
  169.         }
  170.         digit = cvtIn[digit];
  171.         if (digit >= base) {
  172.         break;
  173.         }
  174.         result = result*base + digit;
  175.         anyDigits = TRUE;
  176.     }
  177.     }
  178.  
  179.     /*
  180.      * See if there were any digits at all.
  181.      */
  182.  
  183.     if (!anyDigits) {
  184.     p = string;
  185.     }
  186.  
  187.     if (endPtr != NULL) {
  188.     *endPtr = p;
  189.     }
  190.  
  191.     return result;
  192. }
  193.